package org.budo.graph.entity;

import java.util.LinkedHashMap;
import java.util.Map;

import lombok.Getter;
import lombok.Setter;

/**
 * @author li
 */
@Getter
@Setter
public class CacheGraph {
    public static final String GRAPH_TYPE = "CACHE";

    private static final String LOCAL_HEAP_HIT_COUNT = "LocalHeapHitCount", KEY_COUNT = "KeyCount", CACHE_MISS_COUNT = "CacheMissCount", CACHE_EXPIRED_COUNT = "CacheExpiredCount";

    public static final String[] TITLES = { LOCAL_HEAP_HIT_COUNT, KEY_COUNT, CACHE_MISS_COUNT, CACHE_EXPIRED_COUNT };

    private Long localHeapHitCount;

    private Long keyCount;

    private Long cacheMissCount;

    private Long cacheExpiredCount;

    public CacheGraph(EhcacheStatistics ehcacheStatistics) {
        this.setKeyCount(ehcacheStatistics.getKeyCount().longValue());
        this.setLocalHeapHitCount(ehcacheStatistics.getLocalHeapHitCount());
        this.setCacheMissCount(ehcacheStatistics.getCacheMissCount());
        this.setCacheExpiredCount(ehcacheStatistics.getCacheExpiredCount());
    }

    public Map<String, String> minus(CacheGraph lastCacheGraph) {
        Map<String, String> minusMap = new LinkedHashMap<String, String>(); // 这里应该是需要有序的Map的，顺序应该和 TITLES 一致
        minusMap.put(LOCAL_HEAP_HIT_COUNT, (this.getLocalHeapHitCount() - lastCacheGraph.getLocalHeapHitCount()) + ""); // 这里存储的是增量
        minusMap.put(KEY_COUNT, this.getKeyCount() + ""); // keyCount存储的是总量，不是增量

        minusMap.put(CACHE_MISS_COUNT, (this.getCacheMissCount() - lastCacheGraph.getCacheMissCount()) + ""); // 增量
        minusMap.put(CACHE_EXPIRED_COUNT, (this.getCacheExpiredCount() - lastCacheGraph.getCacheExpiredCount()) + ""); // 增量
        return minusMap;
    }
}